<template>
  <div id="main">
    <h2>执行情况管理</h2>

    <div class="head">
      <el-button
        type="primary"
        style="margin:20px"
        @click="handleOpen('添加执行情况', 1)"
      >添加执行情况</el-button>
      <el-input
        v-model="searchTableInfo"
        placeholder="请输入搜索内容"
        style="width:240px;float: right;margin:20px;"
      />
    </div>
    <div id="table-area">
      <el-table
        v-loading="listLoading"
        :data="list.slice((currentPage - 1) * pagesize, currentPage * pagesize)"
        border
        fit
        highlight-current-row
      >
        <el-table-column label="序号" type="index" align="center">
          <template scope="scope">
            <span>{{ (currentPage - 1) * pagesize + scope.$index + 1 }}</span>
          </template>
        </el-table-column>

        <el-table-column
          align="center"
          prop="staffName"
          label="执行人"
        />

        <el-table-column
          align="center"
          prop="executiveConditionSummarize"
          label="课后小结"
        />
        <el-table-column
          align="center"
          prop="executiveConditionTime"
          label="执行时间"
        />
        <el-table-column align="center" label="操作">
          <template slot-scope="Scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              @click="handleOpen('修改计划', 2, Scope.row.executiveConditionId)"
            >编辑</el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              @click="
                handleDelete(Scope.row.executiveConditionId)
              "
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div id="pages">
        <el-pagination
          :current-page="currentPage"
          :page-sizes="[5, 10]"
          :page-size="pagesize"
          layout="total, sizes,prev, pager, next"
          :total="list.length"
          prev-text="上一页"
          next-text="下一页"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>

    <el-dialog
      title=""
      :close-on-click-modal="false"
      :visible.sync="dialogFormVisible"
      :before-close="handleClose"
    >
      <el-form
        ref="executiveCondition"
        :model="executiveCondition"
        label-position="top"
        :rules="rules"
      >

        <el-form-item
          prop="executiveConditionTeacherId"
          label="授课教师姓名"
          :label-width="formLabelWidth"
        >
          <el-select
            v-model="executiveCondition.executiveConditionTeacherId"
            filterable
            placeholder="请选择"
            style="width: 100%;"
          >
            <el-option
              v-for="item in teacher"
              :key="item.staffId"
              :label="item.String"
              :value="item.staffId"
            />
          </el-select>
        </el-form-item>

        <el-form-item prop="executiveConditionPrepare" label="课前准备" :label-width="formLabelWidth">
          <el-input
            v-model="executiveCondition.executiveConditionPrepare"
            autocomplete="off"
            maxlength="500"
            show-word-limit
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 20 }"
            placeholder="请输入内容"
          />
        </el-form-item>

        <el-form-item prop="executiveConditionRecord" label="课堂记录" :label-width="formLabelWidth">
          <el-input
            v-model="executiveCondition.executiveConditionRecord"
            autocomplete="off"
            maxlength="500"
            show-word-limit
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 20 }"
            placeholder="请输入内容"
          />
        </el-form-item>

        <el-form-item prop="executiveConditionStudentId" label="学生" :label-width="formLabelWidth">
          <el-select v-model="executiveCondition.executiveConditionStudentId" filterable placeholder="请选择" style="width: 40%;">
            <el-option
              v-for="item in child"
              :key="item.studentId"
              :label="item.String"
              :value="item.studentId"
            />
          </el-select>
        </el-form-item>

        <el-form-item prop="executiveConditionSummarize" label="课后小结" :label-width="formLabelWidth">
          <el-input
            v-model="executiveCondition.executiveConditionSummarize"
            autocomplete="off"
            maxlength="500"
            show-word-limit
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 20 }"
            placeholder="请输入内容"
          />
        </el-form-item>

        <el-form-item prop="executiveConditionTeachchat" label="教学讨论" :label-width="formLabelWidth">
          <el-input
            v-model="executiveCondition.executiveConditionTeachchat"
            autocomplete="off"
            maxlength="500"
            show-word-limit
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 20 }"
            placeholder="请输入内容"
          />
        </el-form-item>

        <el-form-item prop="executiveConditionPeriodchat" label="阶段讨论" :label-width="formLabelWidth">
          <el-input
            v-model="executiveCondition.executiveConditionPeriodchat"
            autocomplete="off"
            maxlength="500"
            show-word-limit
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 20 }"
            placeholder="请输入内容"
          />
        </el-form-item>

        <el-form-item
          label="执行时间"
          :label-width="formLabelWidth"
          prop="executiveConditionTime"
        >
          <el-date-picker
            v-model="executiveCondition.executiveConditionTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择日期时间"
            style="width: 100%;"
          />
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button
          v-if="editType == 2"
          type="primary"
          @click="handleUpdate('executiveCondition')"
        >修 改</el-button>
        <el-button
          v-if="editType == 1"
          type="primary"
          @click="handleAddUser('executiveCondition')"
        >添 加</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { getAllexecutiveCondition, addexecutiveCondition, deleteexecutiveCondition, updateexecutiveCondition, getexecutiveConditionById } from '@/api/executiveCondition'
import { getStaffStringT } from '@/api/staff'
import { selectStudent } from '@/api/child'
import { validateChineseName, validatePhoneTwo, validateEMail, checkSpecificKey, validateCharacter, btKeyUp, isInteger } from '@/api/validata'

export default {
  data() {
    return {
      newexecutiveCondition: {
        executiveConditionAdmissionAssessment: '',
        executiveConditionId: '',
        staffName: ''
      },
      value: '',
      currentPage: 1, // 默认显示页面为1
      pagesize: 5, //    每页的数据条数
      // list: [],
      tableList: [],
      teacher: [],
      searchTableInfo: '',
      executiveCondition: {},
      listLoading: false,
      dialogFormVisible: false,
      editType: 1, // 1 添加 2 修改
      child: {},
      formLabelWidth: '200px',
      rules: {
        executiveConditionStudentId: [
          { required: true, message: '学生不能为空', trigger: 'blur' }
        ],
        executiveConditionTeacherId: [
          { required: true, message: '授课教师不能为空', trigger: 'blur' }
        ],
        executiveConditionPrepare: [
          { required: true, message: '课前准备不能为空', trigger: 'blur' }
          // {validator:validateChineseName, trigger: ['blur', 'change']},
        ],
        executiveConditionRecord: [
          { required: true, message: '课堂记录不能为空', trigger: 'blur' }
          // {validator:validateChineseName, trigger: ['blur', 'change']},
        ],
        executiveConditionSummarize: [
          { required: true, message: '课后小结不能为空', trigger: 'blur' }
          // {validator:validateChineseName, trigger: ['blur', 'change']},
        ],
        executiveConditionTeachchat: [
          { required: true, message: '教学讨论不能为空', trigger: 'blur' }
          //  {validator:validateChineseName, trigger: ['blur', 'change']},
        ],
        executiveConditionPeriodchat: [
          { required: true, message: '阶段讨论不能为空', trigger: 'blur' }

        ],
        executiveConditionTime: [
          { required: true, message: '执行时间不能为空', trigger: 'blur' }
          // {validator:validateChineseName, trigger: ['blur', 'change']},
        ]
      }
    }
  },
  computed: {
    // 根据计算属性模糊搜索
    list() {
      const searchTableInfo = this.searchTableInfo
      console.log(this.searchTableInfo)
      if (searchTableInfo) {
        // filter() 方法创建一个新的数组，新数组中的元素是通过检查指定数组中符合条件的所有元素。
        // 注意： filter() 不会对空数组进行检测。
        // 注意： filter() 不会改变原始数组。
        return this.tableList.filter(data => {
          console.log('success' + data)
          // some() 方法用于检测数组中的元素是否满足指定条件;
          // some() 方法会依次执行数组的每个元素：
          // 如果有一个元素满足条件，则表达式返回true , 剩余的元素不会再执行检测;
          // 如果没有满足条件的元素，则返回false。
          // 注意： some() 不会对空数组进行检测。
          // 注意： some() 不会改变原始数组。
          return Object.keys(data).some(key => {
            // indexOf() 返回某个指定的字符在某个字符串中首次出现的位置，如果没有找到就返回-1；
            // 该方法对大小写敏感！所以之前需要toLowerCase()方法将所有查询到内容变为小写。
            return (
              String(data[key])
                .toLowerCase()
                .indexOf(searchTableInfo) > -1
            )
          })
        })
      }
      return this.tableList
    }
  },
  created() {
    this.fetchData()
    this.getinfo()
    this.getAllStaffTeacher()
  },
  methods: {
    // 每页下拉显示数据
    handleSizeChange: function(size) {
      this.pagesize = size
    },
    // 点击第几页
    handleCurrentChange: function(currentPage) {
      this.currentPage = currentPage
    },
    getinfo: function() {
      selectStudent().then((resp) => {
        this.child = resp.obj
        console.log(this.child)
      })
    },

    getAllStaffTeacher() {
      getStaffStringT().then(res => {
        console.log(res)
        this.teacher = res.obj
      })
    },
    handleClick(row) {
      console.log(row)
    },
    fetchData() {
      this.tableList = []
      this.listLoading = true
      getAllexecutiveCondition().then(data => {
        this.newexecutiveCondition
        console.log('data的数据', data)
        for (var i = 0; i < data.obj.length; i++) {
          this.newexecutiveCondition.executiveConditionId = data.obj[i].executiveConditionId
          this.newexecutiveCondition.executiveConditionSummarize = data.obj[i].executiveConditionSummarize
          this.newexecutiveCondition.executiveConditionTime = data.obj[i].executiveConditionTime
          this.newexecutiveCondition.staffName = data.obj[i].staff.staffName
          this.tableList.push(this.newexecutiveCondition)
          this.newexecutiveCondition = {}
        }
        console.log('tablelist的数据', this.tableList)
        this.listLoading = false
      })
    },
    handleDelete(id) {
      this.$confirm('确认删除该情况？').then(() =>
        deleteexecutiveCondition(id).then(data => {
          this.$notify({
            title: '操作成功',
            type: 'success'
          })
          const totalPage = Math.ceil(
            (this.tableList.length - 1) / this.pagesize
          )
          const page =
            this.currentPage > totalPage ? totalPage : this.currentPage
          this.currentPage = this.currentPage < 1 ? 1 : page
          this.fetchData()
        })
      )
    },
    handleAddUser(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          addexecutiveCondition(this.executiveCondition).then(resp => {
            this.$notify({
              title: '操作成功',
              type: 'success'
            })
            this.fetchData()
          })
          this.executiveCondition = {}
          this.dialogFormVisible = false
          return true
        } else {
          this.$notify({
            title: '请正确填写',
            type: 'error'
          })
          return false
        }
      })
    },
    handleOpen(title, type, id) {
      this.executiveCondition = {}
      this.title = title
      this.editType = type
      if (type === 2) {
        getexecutiveConditionById(id).then((resp) => {
          this.executiveCondition = resp.obj
          this.staff.staffName = resp.obj.staff.staffName
          console.log('点击修改后的信息', resp.obj)
        })
        this.dialogFormVisible = true
      } else {
        this.dialogFormVisible = true
      }
    },
    handleClose() {
      this.dialogFormVisible = false
      this.executiveCondition = {}
    },

    handleUpdate(formName) {
      this.$refs[formName].validate(valid => {
        console.log(this.executiveCondition)
        if (valid) {
          updateexecutiveCondition(this.executiveCondition).then(resp => {
            this.$notify({
              title: '修改成功',
              type: 'success'
            })
            this.fetchData()
          }).catch(resp => {
            this.fetchData()
          })

          this.dialogFormVisible = false
          return true
        } else {
          this.$notify({
            title: '请正确填写',
            type: 'error'
          })
          return false
        }
      })
    }
    // handleUpdate(formName) {
    //   this.$refs[formName].validate(valid => {
    //     if (valid) {
    //       console.log(this.executiveCondition)
    //       updateexecutiveCondition(this.executiveCondition).then(resp => {
    //         this.$notify({
    //           title: '修改成功',
    //           type: 'success'
    //         })
    //         this.fetchData()
    //       })
    //       this.dialogFormVisible = false
    //       return true
    //     } else {
    //       this.$notify({
    //         title: '请正确填写',
    //         type: 'error'
    //       })
    //       return false
    //     }
    //   })
    // }
    // getAllJurisdiction() {
    //   getAllJurisdiction().then((resp) => {
    //     this.jurisdiction = resp.obj
    //   })
    // },
  }
}
</script>

<style scoped>
#main {
  margin-left: 5px;
  margin-right: 5px;
}
#table-area {
  margin-left: 10px;
  margin-right: 10px;
}
</style>
